import threading
import queue
import time


def producer(q):
    num = 0
    for i in range(10):
        num += 1
        q.put(f"产品{num}")
        print(f"生产了产品{num}")

    while True:
        if q.qsize() < 100:
            num += 1
            q.put(f"产品{num}")
            print(f"生产了产品{num}")
        else:
            break
    print("生产者结束")


def consumer(q):
    while True:
        if not q.empty():
            item = q.get()
            print(f"消费了产品{item}")
            time.sleep(0.0001)
        else:
            break
    print("消费者结束")


if __name__ == "__main__":
    q = queue.Queue()
    t1 = threading.Thread(target=producer, args=(q,))
    t2 = threading.Thread(target=consumer, args=(q,))
    t1.start()
    # time.sleep(0.01)  # 确保生产者先启动
    t2.start()
    t1.join()
    t2.join()
